import { Injectable } from '@angular/core'
import { TreeModel } from 'ng2-tree'
import { Http } from '@angular/http'
import { ToolsTreeNode } from '../data_model/data-model'

@Injectable()
export class ToosTreeService {
    private RsltTree: TreeModel;

    constructor(private http: Http) { }

    getToolsTree(): Promise<any> {
        // return this.organizeTree();
        return this.organizeSettingTree();
    }

    getToolsSettingTree(): Promise<any> {
        return this.organizeSettingTree();
    }

    addTool(toolsTreeNode: ToolsTreeNode): Promise<any> {
        return new Promise((resolve, reject) => {
            this.http.post('/modelItem/create', null, {
                search: {
                    'modelId': toolsTreeNode.modelId,
                    'value': toolsTreeNode.value,
                    'parentId': toolsTreeNode.parentId
                }
            }).toPromise().then(res => {
                resolve(res);
            }, error => {
                reject(error);
            })
        })
    }

    updateTool(toolsTreeNode: ToolsTreeNode): Promise<any> {
        return new Promise((resolve, reject) => {
            this.http.post('/modelItem/update', null, {
                search: {
                    'id': toolsTreeNode.id,
                    'modelId': toolsTreeNode.modelId,
                    'value': toolsTreeNode.value,
                    'parentId': toolsTreeNode.parentId
                }
            }).toPromise().then(res => {
                resolve(res.json().data);
            }, error => {
                reject(error);
            })
        })
    }
    deleteTool(toolId: string): Promise<any> {
        return new Promise((resolve, reject) => {
            this.http.delete('/modelItem/delete', {
                search: {
                    'id': toolId
                }
            }).toPromise().then(res => {
                resolve(res.json().data);
            }, error => {
                reject(error);
            })
        })
    }

    /////private//////////////////
    private organizeTree(): TreeModel {
        this.RsltTree = {
            value: "Geographical Tools",
            settings: {
                cssClasses: {
                    expanded: 'fa fa-caret-down',
                    collapsed: 'fa fa-caret-right',
                    empty: 'fa fa-caret-right disabled',
                    leaf: 'fa fa-lg'
                },
                templates: {
                    node: '<i class="fa fa-archive"></i>',
                    leaf: '<i class="fa fa-wrench"></i>'
                },
                static: true
            },
            children: [
                {
                    value: "Analysis Tools",
                    id: 1,
                    loadChildren: (callback) => {
                        callback([
                            {
                                value: "Extract",
                                id: 11,
                                loadChildren: (callback) => {
                                    callback([
                                        {
                                            value: "Clip",
                                            id: '59f57d0fc38772305035333f'
                                        },
                                        {
                                            value: "Select",
                                            id: '59f981e55c482c3958e7c022'
                                        },
                                        {
                                            value: "Split",
                                            id: '59f981e55c482c3958e7c022'
                                        }, {
                                            value: "Table Select",
                                            id: '59f981e55c482c3958e7c022'
                                        }
                                    ])
                                }
                            },
                            {
                                value: "Overlay",
                                loadChildren: (callback) => {


                                    callback([
                                    ])
                                }
                            },
                            {
                                value: "Proximity",
                                loadChildren: (callback) => {
                                    callback([
                                    ])
                                }
                            },
                            {
                                value: "Statistics",
                                loadChildren: (callback) => {
                                    callback([
                                    ])
                                }
                            }
                        ]);
                    }
                }
            ]

        }



        return this.RsltTree;
    }

    private organizeSettingTree(): Promise<any> {
        return new Promise((resolve, reject) => {
            this.http.get('/modelItem/list', {
                search: {
                    queryCondition: 'all',
                    value: 'NULL'
                }
            }).toPromise().then(data => {
                let ResponseData: Array<ToolsTreeNode> = data.json().data;

                let RootNodeArray: Array<ToolsTreeNode> = ResponseData.filter(value => {
                    return value.parentId === 'NULL';
                })
                //组装树
                RootNodeArray.forEach((value, index) => {
                    this.generateTree(value, ResponseData);
                });

                resolve(RootNodeArray);



            },error=>{
                reject(error);
            })
        })






    }

    private generateTree(RootNode: ToolsTreeNode, AllTreeNodes: Array<ToolsTreeNode>) {
        let childNodes: Array<ToolsTreeNode> = AllTreeNodes.filter((element, index, array) => {
            return element.parentId === RootNode.id;   //注意别写成一个=
        });
        if (childNodes.length > 0) {
            RootNode.children = childNodes;
            childNodes.forEach((value, index) => {
                this.generateTree(value, AllTreeNodes);
            })
        }
    }
}